| Conditions | 1 |
| Paths | 1 |
| Total Lines | 92 |
| Code Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import jQuery from 'jquery'; |
||
| 9 | jQuery.fn.gdprCookieLawPopup = (function () { |
||
| 10 | const _self = this; |
||
| 11 | |||
| 12 | _self.params = { |
||
| 13 | agreementExpiresInDays: 30, |
||
| 14 | autoAcceptCookiePolicy: false |
||
| 15 | }; |
||
| 16 | |||
| 17 | _self.vars = { |
||
| 18 | INITIALISED: false, |
||
| 19 | COOKIE_NAME: 'GDPR_COOKIE_LAW_CONSENT' |
||
| 20 | }; |
||
| 21 | |||
| 22 | // Overwrite default parameters if any of those is present |
||
| 23 | const parseParameters = function (settings) { |
||
| 24 | if (settings) { |
||
| 25 | if (typeof settings.agreementExpiresInDays !== 'undefined') { |
||
| 26 | _self.params.agreementExpiresInDays = settings.agreementExpiresInDays; |
||
| 27 | } |
||
| 28 | if (typeof settings.autoAcceptCookiePolicy !== 'undefined') { |
||
| 29 | _self.params.autoAcceptCookiePolicy = settings.autoAcceptCookiePolicy; |
||
| 30 | } |
||
| 31 | } |
||
| 32 | }; |
||
| 33 | |||
| 34 | // Storing the consent in a cookie |
||
| 35 | const setUserAcceptsCookies = function (consent) { |
||
| 36 | let d = new Date(); |
||
| 37 | const expiresInDays = _self.params.agreementExpiresInDays * 24 * 60 * 60 * 1000; |
||
| 38 | d.setTime(d.getTime() + expiresInDays); |
||
| 39 | const expires = "expires=" + d.toGMTString(); |
||
| 40 | document.cookie = _self.vars.COOKIE_NAME + '=' + consent + "; " + expires + ";path=/"; |
||
| 41 | }; |
||
| 42 | |||
| 43 | // Let's see if we have a consent cookie already |
||
| 44 | const userAlreadyAcceptedCookies = function () { |
||
| 45 | let userAcceptedCookies = false; |
||
| 46 | const cookies = document.cookie.split(";"); |
||
| 47 | for (let i = 0; i < cookies.length; i++) { |
||
| 48 | const c = cookies[i].trim(); |
||
| 49 | if (c.indexOf(_self.vars.COOKIE_NAME) == 0) { |
||
|
|
|||
| 50 | userAcceptedCookies = c.substring(_self.vars.COOKIE_NAME.length + 1, c.length); |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | return userAcceptedCookies; |
||
| 55 | }; |
||
| 56 | |||
| 57 | const hideContainer = function () { |
||
| 58 | jQuery('.gdprpopup-container').animate({ |
||
| 59 | opacity: 0, |
||
| 60 | height: 0 |
||
| 61 | }, 200, function () { |
||
| 62 | jQuery('.gdprpopup-container').hide(0); |
||
| 63 | }); |
||
| 64 | }; |
||
| 65 | |||
| 66 | return { |
||
| 67 | init: function (settings) { |
||
| 68 | parseParameters(settings); |
||
| 69 | |||
| 70 | // No need to display this if user already accepted the policy |
||
| 71 | if (userAlreadyAcceptedCookies()) { |
||
| 72 | return; |
||
| 73 | } |
||
| 74 | |||
| 75 | // We should initialise only once |
||
| 76 | if (_self.vars.INITIALISED) { |
||
| 77 | return; |
||
| 78 | } |
||
| 79 | _self.vars.INITIALISED = true; |
||
| 80 | |||
| 81 | jQuery('.gdprpopup-button-confirm').click(function () { |
||
| 82 | setUserAcceptsCookies(true); |
||
| 83 | hideContainer(); |
||
| 84 | return false; |
||
| 85 | }); |
||
| 86 | jQuery('.gdprpopup-closebutton').click(function () { |
||
| 87 | hideContainer(); |
||
| 88 | return false; |
||
| 89 | }); |
||
| 90 | |||
| 91 | // Ready to start! |
||
| 92 | jQuery('.gdprpopup-container').show(); |
||
| 93 | |||
| 94 | // In case it's alright to just display the message once |
||
| 95 | if (_self.params.autoAcceptCookiePolicy) { |
||
| 96 | setUserAcceptsCookies(true); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | }; |
||
| 100 | }); |
||
| 101 | }(jQuery)); |
||
| 107 |